package org.itzixi.netty.utils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class PropertiesUtils {
    private static final String CHAT_MESSAGE_PROPERTIES_PATH = "chat-message.properties";

    private static volatile Properties INSTANCE;


    private PropertiesUtils(){

    }

//    public static Properties getInstance(){
//        if (INSTANCE == null){
//            synchronized (PropertiesUtils.class){
//                if (INSTANCE == null){
//                    INSTANCE = getProperties();
//                }
//            }
//        }
//        return INSTANCE;
//    }

    public static Properties getInstance() {
        if (INSTANCE == null){
            synchronized (PropertiesUtils.class){
                if (INSTANCE == null){
                    INSTANCE = new Properties();
                    loadPropertiesFile(INSTANCE);
                }
            }
        }

        return INSTANCE;
    }

    private static void loadPropertiesFile(Properties properties){
        InputStream inputStream = PropertiesUtils.class.getClassLoader().getResourceAsStream(CHAT_MESSAGE_PROPERTIES_PATH);

        //*.properties配置文件，要使用UTF-8编码，否则会现中文乱码问题
        assert inputStream != null;
        try {
//            FileInputStream fs = new FileInputStream("src/main/resources/chat-message.properties");
            BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream,StandardCharsets.UTF_8));

            properties.load(bf);
        } catch (IOException e) {
//            log.error("读取配置" + TEST_PROPERTIES_URI + "文件异常", e);
            throw new RuntimeException(e);
        }
    }




}
